package com.syx.friend.Manager;

import ch.qos.logback.core.joran.conditional.IfAction;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.syx.common.redis.Service.RedisService;
import com.syx.commoncore.Constant.CacheConstant;
import com.syx.commoncore.Constant.Constant;
import com.syx.commoncore.enums.Resultcode;
import com.syx.friend.Mapper.QuestionMapper;
import com.syx.friend.Model.question.Question;
import com.syx.friend.Model.question.VO.QuestionVO;
import com.syx.security.Exception.ServiceException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;
@Slf4j
@Component
public class QuestionCacheManager {
    @Resource
    RedisService redisService;
    @Resource
    QuestionMapper questionMapper;

    public Long getListSize(){
        //获取题目列表的个数
        return redisService.getListSize(CacheConstant.QUESTION_LIST);
    }


    public List<QuestionVO> getQuestions(String key) {
        List<QuestionVO> questionVOS = redisService.getCacheListByRange(key,0,-1,QuestionVO.class);
        // 使用FastJSON的TypeReference指定泛型类型
        return questionVOS;
    }


    public void refreshCache() {
        //从主存储机制中get
        //降序排列
        List<Question> questionList = questionMapper.selectList(new LambdaQueryWrapper<Question>().select(Question::getQuestionId).orderByDesc(Question::getCreateTime));
        if (CollectionUtil.isEmpty(questionList)){
            //如果判断数据库中也啥题目都没有，那就别刷了直接返回；
            return;

        }
        //刷数据，获取数据
        List<Long> collect = questionList.stream().map(Question::getQuestionId).toList();
        redisService.rightPushAll(CacheConstant.QUESTION_LIST,collect);


    }



    public Long preQuestion(Long questionId) {

        Long index = redisService.indexOfForList(CacheConstant.QUESTION_LIST, questionId);
        //有可能是第一题
        if (index==0){
            throw new ServiceException(Resultcode.FAILED_FIRST_QUESTION);
        }
        //获取上一题的id
        return redisService.indexForList(CacheConstant.QUESTION_LIST, index - 1, Long.class);



    }

    public Long nextQuestion(Long questionId) {
        Long index = redisService.indexOfForList(CacheConstant.QUESTION_LIST, questionId);
        //有可能是最后的题
        Long lastIndex = getListSize()-1;
        if (index.equals(lastIndex)){
            throw new ServiceException(Resultcode.FAILED_LAST_QUESTION);
        }
        //获取下一题的id
        return redisService.indexForList(CacheConstant.QUESTION_LIST, index +1, Long.class);
    }

    public Long getHostListSize() {
        return redisService.getListSize(CacheConstant.QUESTION_HOST_LIST);

    }

    public void refreshHotListCache(List<QuestionVO> questionVOS) {
        //将questionvo放入redis中并设置时间
        if (CollectionUtil.isEmpty(questionVOS)){
            return;
        }
        //开刷
        redisService.rightPushAll(CacheConstant.QUESTION_HOST_LIST,questionVOS);
        redisService.expire(CacheConstant.QUESTION_HOST_LIST,1, TimeUnit.DAYS);




    }
}
